/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/17 22:49
 */
package org.mechanic.fund.web.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mechanic.core.web.MediaTypes;
import org.mechanic.fund.domain.system.manager.Permission;
import org.mechanic.fund.service.product.impl.PermissionService;
import org.mechanic.fund.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限接口
 *
 * @author mechanic
 * @version 0.0.1
 */
@Api("权限信息接口")
@RestController
@RequestMapping(value = "permission")
public class SystemPermissionController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SystemPermissionController.class);

    private PermissionService permissionService;

    @Autowired
    public SystemPermissionController(PermissionService permissionService) {this.permissionService = permissionService;}

    @ApiOperation(value = "返回匹配权限编号的权限信息")
    @GetMapping(value = "{id}", produces = MediaTypes.JSON_UTF_8)
    public Object details(
      @ApiParam(value = "权限编号", required = true) @PathVariable(required = true) String id
    ) {
        Permission permission = this.permissionService.findById(id);
        return result(permission);
    }

    @ApiOperation(value = "返回权限信息分页列表")
    @GetMapping(value = "list", produces = MediaTypes.JSON_UTF_8)
    public Object list(
      @ApiParam(value = "页码(从0开始)", required = false) @RequestParam(required = false, defaultValue = "0") Integer page
    ) {
        Page<Permission> permissionPage =this.permissionService.getList(super.pageRequest(page));
        return result(permissionPage);
    }

    @ApiOperation(value = "删除匹配权限编号的权限")
    @GetMapping(value = "delete/{id}", produces = MediaTypes.JSON_UTF_8)
    public Object delete(
      @ApiParam(value = "权限编号", required = true) @PathVariable(required = true) String id
    ) {
        this.permissionService.delete(id);
        return result();
    }

    @ApiOperation(value = "保存权限信息")
    @PostMapping(value = "save", consumes = MediaTypes.JSON_UTF_8)
    public Object save(
      @ApiParam(value = "权限信息实体", required = true) @RequestBody List<Permission> permissions
    ) {
        this.permissionService.save(permissions, super.currUser().getId());
        return result();
    }
}
